from rest_framework import serializers
from apps.article.models import Channel, Article, Comment

# 1 频道列表序列化器
from apps.users.models import User


class ChannelsListSerializer(serializers.ModelSerializer):
    class Meta:
        model = Channel
        fields = '__all__'

# 文章视图序列化器
class ArticleSerializer(serializers.ModelSerializer):
    user = serializers.CharField(read_only=True)

    class Meta:
        model = Article
        fields = '__all__'

# 2 文章列表序列化器
class ArticleSerializerList(serializers.ModelSerializer):

    class Meta:
        model = Article
        fields = '__all__'
        # fields = ['id','title','content','createtime','user','collected_users','collected',' image','visits']

# 4 文章详情序列化器


class ArticlesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article

        fields = ('id','title')


# 用户详情序列化器
class UserDetailSerializer(serializers.ModelSerializer):
    articles = ArticlesSerializer(many=True)
    class Meta:
        model = User
        fields = ('id','username','avatar','articles','fans')

class SubCommentss(serializers.ModelSerializer):
    user = UserDetailSerializer(read_only=True)
    class Meta:
        model = Comment
        fields = ['id','content','article','user','parent','createtime']

class SubComments(serializers.ModelSerializer):
    user = UserDetailSerializer(read_only=True)
    subs = SubCommentss(many=True)
    class Meta:
        model = Comment
        fields = ['id','content','article','user','parent','subs','createtime']
class SubComment(serializers.ModelSerializer):
    user = UserDetailSerializer(read_only=True)
    subs = SubComments(many=True)
    class Meta:
        model = Comment
        fields = ['id','content','article','user','parent','subs','createtime']


# 评论列表序列化器
class CommentListSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        fields =['id','user','avatar','fans']
class CommentsListSerializer(serializers.ModelSerializer):
    user = UserDetailSerializer(read_only=True)
    article = serializers.PrimaryKeyRelatedField(read_only=True)
    subs = SubComment(many=True)
    partent = serializers.PrimaryKeyRelatedField(read_only=True)
    class Meta:
        model = Comment
        fields = ['id','user','subs','content','createtime','updatetime','partent','article']
# 详情总序列化器
class ArticleSerializerDetail(serializers.ModelSerializer):

    user = UserDetailSerializer(read_only=True)
    # comments = serializers.SerializerMethodField()
    comments = CommentsListSerializer(many=True)
    labels = serializers.SerializerMethodField()
    collected_users = serializers.SerializerMethodField()

    class Meta:
        model = Article
        fields = '__all__'

    def get_comment(self,obj):
        comments = Comment.objects.filter(article=obj).filter(parent=None)
        return CommentListSerializer(comments,many=True).data

    def get_labels(self,obj):
        data = obj.labels.all()
        #labels = Label.objects.all()
        labels_list = []
        for i in data:
            labels_list.append(i.id)
        return labels_list

    def get_collected_users(self,obj):
        data = obj.collected_users.all()
        users_list = []
        for i in data:
            users_list.append(i.username)
        return users_list

# 5 发布文章序列化器
class ArticleSerializerCreate(serializers.ModelSerializer):
    image = serializers.CharField(required=False,default='',allow_blank=True)

    class Meta:
        model = Article
        exclude = ('collected_users',)

# 6 发布文章评论序列化器

class ArticleCommentSerializer(serializers.ModelSerializer):

    class Meta:
        model = Comment
        fields = '__all__'
